{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ce715bed",
   "metadata": {},
   "source": [
    "### map/reduce"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f389a166",
   "metadata": {},
   "source": [
    "map函数接收两个参数，一个是函数，一个是Iterable，map将函数依次作用到序列的每个元素\n",
    "并把结果作为新的Iterator返回。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c40f76ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(x):\n",
    "    return x * x\n",
    "r = map(f, [1,2,3,4,5,6])\n",
    "list(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5873b4a6",
   "metadata": {},
   "source": [
    "map作为告诫函数，把运算规则抽象，可以计算任意复杂的函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1ab83c17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(str,list(range(10))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91f5f7e",
   "metadata": {},
   "source": [
    "reduce的用法，reduce把一个函数作用在一个序列上，这个函数必须接收两个参数，reduce把结果继续和序列的下一个元素做累积计算。"
   ]
  },
  {
   "cell_type": "raw",
   "id": "70357e84",
   "metadata": {},
   "source": [
    "reduce(f,[x1,x2,x3,x4]) = f(f(f(x1, x2),x3),x4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3293efb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from functools import reduce\n",
    "def add(x, y):\n",
    "    return x + y\n",
    "reduce(add, list(range(10)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff49c98f",
   "metadata": {},
   "source": [
    "案例说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51e200ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "Digits = {'0': 0, '1':1, '2':2, '3':3}\n",
    "def str2int(s):\n",
    "    def fn(x, y):\n",
    "        return x * 10 + y\n",
    "    def char2num(s):\n",
    "        return Digits[s]\n",
    "    return reduce(fn, map(char2num, s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "39ea06ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str2int('12')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "13690a7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Digits = {'0': 0, '1':1, '2':2, '3':3}\n",
    "def char2num(s):\n",
    "    return Digits[s]\n",
    "char2num('2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "465a53b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
